<template>
  <div>
    <!-- 面包屑区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card v-loading="isLoading">
      <el-row>
        <el-Button type="primary" @click="showAddCateDilog">添加分类</el-Button>
      </el-row>
      <!-- 表格 -->
      <el-row>
        <tree-table style="margin-top: 15px" :data="catelist" :columns="columns" :selection-type="false"
          :expand-type="false" show-index index-text="#" border :show-row-hover="false">
          <template #isok="{row}">
            <i class="el-icon-success" v-if="row.cat_deleted === false" style="color:lightgreen"></i>
            <i class="el-icon-error" v-else style="color:red"></i>
          </template>
          <template #order={row}>
            <el-tag size="mini" v-if="row.cat_level === 0">一级</el-tag>
            <el-tag type="success" size="mini" v-else-if="row.cat_level === 1">二级</el-tag>
            <el-tag type="warning" size="mini" v-else>三级</el-tag>

          </template>
          <template #operation>
            <el-button type="primary" size="mini" icon="el-icon-edit">编辑</el-button>
            <el-button type="danger" size="mini" icon="el-icon-delete">删除</el-button>
          </template>
        </tree-table>
      </el-row>
      <!-- 分页区域 -->
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
        :current-page.sync="queryInfo.pagenum" :page-sizes="[5, 10, 15]" :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </el-card>

    <el-dialog title="添加分类" :visible.sync="showCateDialogVisible" width="50%" @close="resetClose">
      <el-form label-width="80px" :model="addCateForm" :rules="addCateFormRules" ref="addCateFormRef">
        <el-form-item label="分类名称" prop="cat_name">
          <el-input v-model="addCateForm.cat_name"></el-input>
        </el-form-item>
        <el-form-item label="父级分类">
          <el-cascader v-model="selectList" :options="parentCateList"
            :props="{ expandTrigger: 'hover',label:'cat_name',checkStrictly: true,value:'cat_id' }"
            @change="handleChange" clearable>
          </el-cascader>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="showCateDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveCat">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data () {
    return {
      selectList: [],
      parentCateList: [],
      addCateFormRules: {
        cat_name: [{ required: true, message: '请输入分类名称', trigger: 'blur' }]
      },
      addCateForm: {
        cat_name: '',
        cat_pid: 0,
        cat_level: 0
      },
      showCateDialogVisible: false,
      isLoading: false,
      queryInfo: {
        pagenum: 1,
        pagesize: 10
      },
      catelist: [],
      total: 0,
      columns: [
        {
          label: '分类名称',
          prop: 'cat_name'
        },
        {
          label: '是否有效',
          type: 'template',
          template: 'isok'
        },
        {
          label: '排序',
          type: 'template',
          template: 'order'
        },
        {
          label: '操作',
          type: 'template',
          template: 'operation'
        }
      ]
    }
  },
  methods: {
    saveCat () {
      this.$refs.addCateFormRef.validate(async bool => {
        if (!bool) return console.log('校验失败')
        const { data: res } = await this.$http.post('categories', this.addCateForm)
        if (res.meta.status !== 201) return this.$Message.error('添加失败')
        this.showCateDialogVisible = false
        this.getCateList()
      })
    },
    resetClose () {
      this.addCateForm.cat_name = ''
      this.addCateForm.cat_pid = 0
      this.addCateForm.cat_level = 0
      this.selectList = []
    },
    handleChange () {
      // console.log(this.selectList)
      if (this.selectList.length === 0) {
        this.addCateForm.cat_pid = 0
        this.addCateForm.cat_level = 0
      } else {
        this.addCateForm.cat_level = this.selectList.length
        this.addCateForm.cat_pid = this.selectList[this.selectList.length - 1]
      }
    },
    async showAddCateDilog () {
      const { data: res } = await this.$http.get('categories', { params: { type: 2 } })
      this.parentCateList = res.data
      // console.log(this.parentCateList)
      this.showCateDialogVisible = true
    },

    handleSizeChange (size) {
      this.queryInfo.pagesize = size
      this.getCateList()
    },
    handleCurrentChange (page) {
      this.queryInfo.pagenum = page
      this.getCateList()
    },
    async getCateList () {
      this.isLoading = true
      const { data: res } = await this.$http.get('categories', { params: this.queryInfo })
      if (res.meta.status !== 200) return this.$Message.error('请求失败')
      this.catelist = res.data.result
      this.total = res.data.total
      this.isLoading = false
    }
  },
  computed: {},
  watch: {},
  filters: {},
  components: {},
  created () {
    this.getCateList()
  },
  mounted () { }
}
</script>

<style scoped lang='less'>
</style>
